/**
 * Copyright (c) 2008 michiyasu wada
 * http://www.seyself.com/
 * 
 * Distributed under The MIT License.
 * [http://www.opensource.org/licenses/mit-license.php]
 */

package demo.objects.items 
{
import demo.modules.ScrollBar;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import jp.progression.casts.CastSprite;
import demo.buttons.MenuItemButton;
import jp.progression.commands.AddChild;
import jp.progression.commands.DoTweener;
import jp.progression.commands.Func;
import jp.progression.commands.ParallelList;
import jp.progression.commands.RemoveAllChild;
import jp.progression.commands.SerialList;

public class MenuCast extends CastSprite
{
    public var scrollbar:ScrollBar;
    public var container:CastSprite;
    
    public function MenuCast() 
    {
        var mask = getChildByName("MaskRect");
        container = new CastSprite();
        addChildAt( container, getChildIndex( mask ) );
        
        scrollbar = ScrollBar( getChildByName("_scrollbar") );
        scrollbar.target = container;
        scrollbar.scrollMask = mask;
        scrollbar.margin.left = 2;
        scrollbar.targetMargin.left = 20;
        scrollbar.targetMargin.right = 20;
        scrollbar.speed = 0.3;
    }
    
    public function setData( data:XMLList ):void
    {
        var commands:SerialList = new SerialList();
        var parallels:ParallelList = new ParallelList();
        commands.addCommand(
            new DoTweener( loadingText, { alpha: 0.0, time: 0.6 }, 0 )
        );
        
        var n:int = data.length();
        for (var i:int = 0; i < n; i++ ) {
            parallels.addCommand(
                new AddChild( container , new MenuItemButton( data[i] ) )
            );
        }
        
        parallels.addCommand( new Func( this ,  scrollbar.reset  ) );
        commands.addCommand( parallels );
        commands.execute();
    }
    
    public function removeItems():void
    {
        var commands:SerialList = new SerialList();
        commands.addCommand(
            new RemoveAllChild( container ),
            new DoTweener( loadingText, { alpha: 1.0, time: 0.6 } )
        );
        commands.execute();
    }
    
    override protected function _onCastAdded():void
    {
        alpha = 0.0;
        loadingText.alpha = 0;
        addCommand(
            new DoTweener( this, { alpha: 1.0, time: 0.8 }, 200 ),
            new DoTweener( loadingText, { alpha: 1.0, time: 0.6 }, 200 )
        );
    }
    
    override protected function _onCastRemoved():void
    {
        addCommand(
            new DoTweener( this, { alpha: 0.0, time: 0.3 } )
        );
    }
    
    
}

}